#include "StdAfx.h"
#include "WebKitCtrl.h"
//#include "..\class\webkit\WebApp.h"


CWebKitCtrl::CWebKitCtrl()
{
	m_pWebkit = NULL;

	OnInit += MakeDelegate(this, &CWebKitCtrl::Initialize);
	OnSize += MakeDelegate(this, &CWebKitCtrl::SetCtrlPos);
}

bool CWebKitCtrl::Initialize(void* param)
{

	Create(m_pManager->GetPaintWindow(), _T("WebKit"), UI_WNDSTYLE_CHILD, 0, 0, 0, 0, 0, NULL);

	ShowWindow(false);
//	m_pm = *m_pManager;
	m_pWebkit = BrowserAgent::CreateWebKit(this->m_hWnd);


//	SetBrowserHelper(m_pWebkit);
	
	return true;
}
bool CWebKitCtrl::SetCtrlPos(void* param)
{
/*
	RECT rc;
	rc.left = GetFixedXY().cx;
	rc.top = GetFixedXY().cy;
	rc.right = rc.left + GetFixedWidth();
	rc.bottom = rc.top + GetFixedHeight();
*/
	RECT rcClient;
	GetClientRect(m_pManager->GetPaintWindow(), &rcClient);


	RECT rcItem = GetPos();
	rcItem.left = rcClient.left + rcItem.left;
	rcItem.right = rcClient.left + rcItem.right;

	rcItem.top = rcClient.top + rcItem.top;
	rcItem.bottom = rcClient.top + rcItem.bottom;

	MoveWindow(*this, rcItem.left, rcItem.top, (rcItem.right - rcItem.left), (rcItem.bottom - rcItem.top), TRUE);
 	if (NULL != m_pWebkit)
 	{
		RECT rcPos;
		rcPos.left = 0;
		rcPos.top = 0;
		rcPos.right = rcItem.right - rcItem.left;
		rcPos.bottom = rcItem.bottom - rcItem.top;
		m_pWebkit->ResizeView(&rcPos);
 	}
	
	return true;
}
CWebKitCtrl::~CWebKitCtrl(void)
{
	if (m_pWebkit != NULL)
	{
		delete m_pWebkit;
		m_pWebkit = NULL;
	}	
}

const BrowserAgent* CWebKitCtrl::GetBrowserAgent() const
{
	return m_pWebkit;
}

void CWebKitCtrl::LoadUrl(LPCTSTR url)
{
	if (m_pWebkit != NULL)
	{
		m_pWebkit->LoadUrl(url);
	}
}

void CWebKitCtrl::Navigate(LPCTSTR url)
{
	return LoadUrl(url);
}

void CWebKitCtrl::SetVisible(bool bVisible)
{	
	if (m_pWebkit != NULL)
	{
		ShowWindow(bVisible);
		if (bVisible)
		{
			m_pWebkit->Show();
		}
		else
		{
			m_pWebkit->Hide();
		}
	}
	__super::SetVisible(bVisible);
}

LRESULT CWebKitCtrl::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
	styleValue &= ~WS_CAPTION;
	::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
//	m_pm.Init(m_hWnd);
	
	bHandled = FALSE;
	return 0;
}
LRESULT CWebKitCtrl::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	LRESULT lRes = 0;
	BOOL bHandled = TRUE;
	switch( uMsg ) {
	case WM_CREATE:        lRes = OnCreate(uMsg, wParam, lParam, bHandled); break;
// 	case WM_NCACTIVATE:    lRes = OnNcActivate(uMsg, wParam, lParam, bHandled); break;
// 	case WM_NCCALCSIZE:    lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled); break;
// 	case WM_NCPAINT:       lRes = OnNcPaint(uMsg, wParam, lParam, bHandled); break;
// 	case WM_NCHITTEST:     lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled); break;
// 	case WM_SIZE:          lRes = OnSize(uMsg, wParam, lParam, bHandled); break;

	case WM_WEBAPPCALLBACKEVENT:
	case WM_WEBAPPCALLBACKEXTERNAL:	
	case WM_WEBAPPCALLBACKLOADURL:
	case WM_WEBAPPSETEXTERNALSTRING:
	case WM_WEBAPPSETEXTERNALBOOLEAN:
	case WM_WEBAPPCALLBACK:			
	case WM_WEBAPPCALLFUNC:		
	case WM_WEBAPPCALLFUNCEX:
		return BrowserAgent::BrowserWndProc(m_hWnd, uMsg, wParam, lParam);
		break;
	case WM_CLOSE:
		{
			int f = 0;
		}
		break;
	default:
		bHandled = FALSE;
	}
	if( bHandled ) return lRes;
	return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
}
